package com.winit.openapi.gt.controller.builder;

import com.alibaba.fastjson.JSONObject;
import com.winit.oms.spi.gt.command.SaveGlobalTransferCommand;
import com.winit.oms.spi.gt.vo.GTOrderCustomerAddress;
import com.winit.oms.spi.gt.vo.GlobalTransferOrder;
import com.winit.openapi.util.DateUtil;
import com.winit.openapi.util.ValidateUtil;

public class GlobalTransferOrderBuilder {

    /**
     * 构建第三步提交的信息
     * 
     * @param json 请求的构建数据
     * @return
     */
    public static SaveGlobalTransferCommand buildThirdOrderCommand(JSONObject json) {
        GlobalTransferOrder order = new GlobalTransferOrder();

        if (json.containsKey("id")) {
            order.setId(json.getInteger("id")); // 国际送仓单ID
        }
        if (json.containsKey("globalTransferNo")) {
            order.setSellerOrderNo(json.getString("globalTransferNo"));// 国际送仓单号
        }
        if (json.containsKey("pickServiceCode")) {
            order.setPickServiceCode(json.getString("pickServiceCode"));// 提货服务名称
        }
        if (json.containsKey("pickServiceName")) {
            order.setPickServiceName(json.getString("pickServiceName")); // 提货地址名称
        }
        if (json.containsKey("deliveryAddressCode")) {
            order.setDeliveryAddressCode(json.getString("deliveryAddressCode")); // 提货地址编码
        }
        if (json.containsKey("warehouseCode")) {
            order.setWarehouseCode(json.getString("warehouseCode")); // 验货仓/中转仓编码
        }
        if (json.containsKey("warehouseName")) {
            order.setWarehouseName(json.getString("warehouseName"));
        }
        if (json.containsKey("forecastWarehouseDate")) {
            order.setForecastWarehouseDate(json.getDate("forecastWarehouseDate")); // 自发物流下需要：预计到仓时间
        }
        if (json.containsKey("expressNo")) {
            order.setExpressNo(json.getString("expressNo")); // 自发物流下需要：快递单号
        }

        // 提货信息
        GTOrderCustomerAddress customerAddress = new GTOrderCustomerAddress();
        if (json.containsKey("date") && ValidateUtil.isNotNull(json, "date")) {
            String[] dates = json.get("date").toString().split("\\|");
            customerAddress.setExpectFromDate(DateUtil.parse2Date(dates[0], DateUtil.FULL_DATE_STRING));
            customerAddress.setExpectToDate(DateUtil.parse2Date(dates[1], DateUtil.FULL_DATE_STRING));
        } else if (json.containsKey("formatDate") && ValidateUtil.isNotNull(json, "formatDate")) {
            String[] dates = json.get("formatDate").toString().split("\\|");
            customerAddress.setExpectFromDate(DateUtil.parse2Date(dates[0], DateUtil.FULL_DATE_STRING));
            customerAddress.setExpectToDate(DateUtil.parse2Date(dates[1], DateUtil.FULL_DATE_STRING));
        } else if (json.containsKey("dateExpectFrom") && json.containsKey("dateExpectTo")) {
            customerAddress.setExpectFromDate(DateUtil.parse2Date(json.getString("dateExpectFrom"),
                DateUtil.FULL_DATE_STRING));
            customerAddress.setExpectToDate(DateUtil.parse2Date(json.getString("dateExpectTo"),
                DateUtil.FULL_DATE_STRING));
        }
        order.setDeliveryAddress(customerAddress);

        SaveGlobalTransferCommand command = new SaveGlobalTransferCommand();
        command.setGlobalTransferOrder(order);
        return command;
    }

}
